<?php
class My_Db
{
	public static function checkTableExist($table)
	{
		$dbAdapter = Zend_Registry::get("dpAdapter");
		$tables = $dbAdapter->listTables();
		return in_array($table, $tables);
	}

	public static function checkColumnExist($table, $column)
	{
		
		$dbAdapter = Zend_Registry::get("dpAdapter");
		
		$columns = $dbAdapter->describeTable($table);
		
		return array_key_exists($column,$columns);
	}

	public static function getColumnData($table, $column)
	{
		if(!self::checkColumnExist($table,$column))
		{
			return NULL;
		}
		$db = Zend_Registry::get("dpAdapter");

		$sql = "SELECT $column FROM $table";

		$result = $db->fetchAll($sql);

		foreach ($result as $row)
		{
			$array[] = $row[$column];
		}
		return $array;
	}

	public static function getPrimaryKey($table)
	{
		$keys = self::getPrimaryKeys($table);
		return $keys[0];
	}

	public static function buildQueryData($table, $orderBy = "", $orderDirection="",
	$key = "", $field = "")
	{
		$db = Zend_Registry::get("dpAdapter");
		$select = new Zend_Db_Select($db);
		$select->from($table);
		if(!empty($key) && !empty($field))
		{
			$key.="%";
			$key = $db->quote($key);
			$select->where($field." like ".$key);
		}

		if(!empty($orderBy) && My_Db::checkColumnExist($table,$orderBy))
		{
			if(empty($orderDirection))
			{
				$orderDirection = "asc";
			}
			$select->order($orderBy." ".$orderDirection);
		}
		return $select;
	}

	public static function getWhere($table,$id)
	{
		$key = self::getPrimaryKey($table);
		$where = "$key = $id";
		return $where;
	}

	public static function get($table, $id)
	{
		$where = self::getWhere($table, $id);
		$db = Zend_Registry::get("dpAdapter");
		$sql = 'SELECT * FROM '.$table." WHERE ".$where;
		$result = $db->fetchRow($sql);
		return $result;
	}


	public static function getList($table, $where)
	{
		$db = Zend_Registry::get("dpAdapter");
		$sql = 'SELECT * FROM '.$table." WHERE ".$where;
		$result = $db->fetchAll($sql);
		return $result;
	}

	public static function insert($table,$data)
	{
		$db = Zend_Registry::get("dpAdapter");
		$db->insert($table, $data);
	}


	public static function update($table,$data,$id)
	{
		$where = self::getWhere($table, $id);
		
		$db = Zend_Registry::get("dpAdapter");
		$db->update($table, $data, $where);
	}

	public static function delete($table, $id)
	{
		$where = self::getWhere($table, $id);

		$db = Zend_Registry::get("dpAdapter");
		$db->delete($table, $where);
	}
	

	public static function getPrimaryKeys($table)
	{
		$db = Zend_Registry::get("dpAdapter");
		$columns = $db->describeTable($table);
		foreach ($columns as $col)
		{
			if($col["PRIMARY"])
			{
				$keys[] = $col["COLUMN_NAME"];
			}
		}
		return $keys;
	}

	public static function getWheres($table,$ids)
	{
		if(!is_array($ids))
		{
			$ids = array($ids);
		}
		$keys = self::getPrimaryKeys($table);
		if(count($keys) != count($ids))
		return "";
		$where = " ";
		for ($i = 0;$i< count($ids);$i++)
		{
			$where .= "$keys[$i] = $ids[$i]";
			if($i<count($ids)-1)
			{
				$where.=" and ";
			}
		}
		return $where;
	}

	public static function gets($table, $ids)
	{
		$where = self::getWheres($table, $ids);
		$db = Zend_Registry::get("dpAdapter");
		$sql = 'SELECT * FROM '.$table." WHERE ".$where;
		$result = $db->fetchRow($sql);
		return $result;
	}

	public static function updates($table,$data,$ids)
	{
		$where = self::getWheres($table, $ids);

		$db = Zend_Registry::get("dpAdapter");
		$db->update($table, $data, $where);
	}

	public static function deletes($table, $ids)
	{
		$where = self::getWheres($table, $ids);

		$db = Zend_Registry::get("dpAdapter");
		$db->delete($table, $where);
	}

}
?>